<template>
  <div class="page">
    <t-nav-bar
        title="维修进度"
        left-text="返回"
        left-arrow
        @click-left="handleBack"
    />
    <div class="content">
      <h1>维修进度</h1>
      <div v-if="sortedRecords.length > 0" class="records-container">
        <div v-for="record in sortedRecords" :key="record.maintenanceRecordsId" class="record-card">
          <h3 class="card-title">{{ record.title }}</h3>
          <div class="card-content">
            <p><span>用户编号:</span> {{ record.userNo }}</p>
            <p><span>姓名:</span> {{ record.fullName }}</p>
            <p><span>类型:</span> {{ record.type }}</p>
            <p><span>报修时间:</span> {{ record.repairTime }}</p>
            <p><span>维修地址:</span> {{ record.repairAddress }}</p>
            <p><span>员工工号:</span> {{ record.employeeId }}</p>
            <p><span>员工姓名:</span> {{ record.employeeName }}</p>
            <p><span>派工时间:</span> {{ record.dispatchTime }}</p>
            <p><span>维修状态:</span> {{ record.maintenanceStatus }}</p>
            <p><span>记录时间:</span> {{ formatDate(record.recordTime) }}</p>
            <p><span>维修详情:</span> {{ record.repairDetails }}</p>
            <p><span>更新时间:</span> {{ formatDate(record.updateTime) }}</p>
            <p><span>评价状态:</span> {{  record.evaStatus}}</p>
          </div>
        </div>
      </div>
      <div v-else class="no-records">
        暂无维修记录
      </div>
    </div>
    <div class="fixed-bottom-bar">
      <el-button type="primary" @click="returnToMenu">返回</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex';
import axios from 'axios'
import { ElButton, ElMessage } from 'element-plus'

const router = useRouter()
const records = ref([])

const handleBack = () => {
  router.back()
}

const returnToMenu = () => {
  router.push('/home')
}

const store = useStore();

const fetchRecords = async () => {
  try {
    const user = store.getters.user;
    if (!user || !user.userNo) {
      ElMessage.error('用户未登录或用户信息不完整');
      return;
    }

    const response = await axios.get(`http://localhost:8080/maintenance-records/user/${user.userNo}`);
    records.value = response.data;
  } catch (error) {
    console.error('Error fetching maintenance records:', error);
    ElMessage.error('获取维修记录失败');
  }
};

const formatDate = (dateString: string) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString()
}

const sortedRecords = computed(() => {
  return [...records.value].sort((a, b) => {
    return new Date(b.createTime).getTime() - new Date(a.createTime).getTime()
  })
})

onMounted(() => {
  fetchRecords()
})
</script>

<style scoped>
.page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: Arial, sans-serif;
  background-color: #f0f2f5;
  padding-bottom: 60px;
}

.content {
  flex: 1;
  padding: 15px;
  overflow-y: auto;
}

.records-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 15px;
}

.record-card {
  background-color: white;
  border-radius: 4px;
  padding: 10px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.card-title {
  margin-top: 0;
  margin-bottom: 8px;
  color: #1a1a1a;
  font-size: 16px;
}

.card-content {
  font-size: 12px;
}

.card-content p {
  margin: 4px 0;
  color: #4a4a4a;
}

.card-content span {
  color: #1a1a1a;
  font-weight: bold;
}

.fixed-bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  background-color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
}

.fixed-bottom-bar .el-button {
  width: 80%;
  max-width: 300px;
}

.no-records {
  text-align: center;
  padding: 20px;
  color: #999;
  font-size: 14px;
  background-color: #f9f9f9;
  border-radius: 4px;
  margin-top: 20px;
}
</style>